// 对整棵树进行抽象
class Ref {
  constructor(opt, tree) {
    this.name = opt.name
    this.ref = {}
    this.tree = tree
    refs[this.name] = this
  }

  get(cid) {
    return cid ? this.ref[cid] : this.tree
  }

  set(vm) {
    let cid = vm.cid
    this.ref[cid] = vm
  }
}
// 一整棵树实例
let refs = {}

const init = (opt, tree) => {
  if(!refs[opt.name]) {
    return new Ref(opt, tree)
  }
  return refs[opt.name]
}

const destroy = (name) => {
  refs[name] && delete refs[name]
}

const get = (name, cid) => {
  return refs[name] && refs[name].get(cid)
}

const set = (name, vm) => {
  refs[name] && refs[name].set(vm)
}

export default {
  init,
  destroy,
  set,
  get
}